package io.renren.sf.service.impl;

import io.renren.common.utils.*;
import io.renren.sf.entity.CbBasisStrokesFormworkEntity;
import io.renren.sf.feign.OssFeign;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.File;
import java.util.List;
import java.util.Map;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import io.renren.sf.dao.CbStandardBrushDao;
import io.renren.sf.entity.CbStandardBrushEntity;
import io.renren.sf.service.CbStandardBrushService;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;


@Service("cbStandardBrushService")
public class CbStandardBrushServiceImpl extends ServiceImpl<CbStandardBrushDao, CbStandardBrushEntity> implements CbStandardBrushService {
    @Autowired
    private OssFeign ossFeign;

    @Override
    public void updateBatchByBid(List<CbStandardBrushEntity> standardBrushEntities) {
        baseMapper.updateBatchByBid(standardBrushEntities);
    }

    @Override
    public String createBzbfZip() {
        // 获取基本笔画模板的所有记录
        List<CbStandardBrushEntity> bzbfList = this.baseMapper.selectList(new QueryWrapper<>());
        for (CbStandardBrushEntity standardBrushEntity : bzbfList) {
            // 下载标准笔法视频
            String brushVideo = standardBrushEntity.getStandardBrushVideo();
            if (StringUtils.isEmpty(brushVideo)) {
                return "存在标准笔法视频未添加";
            }
            String brushVideoSuffix = brushVideo.substring(brushVideo.lastIndexOf("."));
            String brushVideoMd5 = MD5.encrypt(brushVideo) + brushVideoSuffix;
            Download.download(brushVideo, brushVideoMd5, Constant.ZipStandardBrush.PATH.getName());
        }

        // 将e:\hanzhiZipStandardBrush\下的文件打成压缩包
        // 得到file数组
        File[] files = getFiles(Constant.ZipStandardBrush.PATH.getName());
        String zipPath = Constant.ZipStandardBrush.YSB.getName() + Constant.ZipStandardBrush.ZIPNAME.getName() + ".zip";
        File zipFile = new File(zipPath); // zip存放路径
        ZipMultiFile.zipFiles(files, zipFile);
        R upload = ossFeign.uploadZip(zipPath);// 上传至oss
        return (String) upload.get("url");
    }

    private File[] getFiles(String path) {
        File dir = new File(path);
        return dir.listFiles();
    }
}